package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.PatentprojectMapper;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.service.*;
import com.hbnu.system.utils.CheckRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class PatentprojectServiceImpl extends ServiceImpl<PatentprojectMapper, Patentproject> implements IPatentprojectService {

    @Autowired
    ITranproject_registrationService iTranproject_registrationService;

    @Autowired
    IPorproject_registrationService iPorproject_registrationService;

    @Autowired
    private IPatentService iPatentService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private CheckRoleUtil checkRoleUtil;


    /**
     * 添加项目
     * @param patentproject 要添加的专利项目
     * @return 新增操作是否成功
     */
    @Override
    public boolean addPatentProject(Patentproject patentproject) {

        //拦截普通用户在非法状态修改的操作
        Patent patent = iPatentService.getOne(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getPatentID, patentproject.getPatentID()));
        checkRoleUtil.checkPermissionAchievement(patent.getStatus());

        if(StrUtil.isEmpty(patentproject.getProjectName()) || StrUtil.isBlank(patentproject.getProjectName())){
            throw new BaseException("请输入项目名称!");
        }

        Porproject_registration porproject = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectName, patentproject.getProjectName()));

        Tranproject_registration tranproject = iTranproject_registrationService.getOne(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectName, patentproject.getProjectName()));

        if(Objects.nonNull(porproject)){
            patentproject.setHead(porproject.getHead());
            patentproject.setProjectID(porproject.getProjectId());
            if(Objects.isNull(patentproject.getStartTime())){
                patentproject.setStartTime(porproject.getStartTime());
            }
            if(Objects.isNull(patentproject.getStartTime())){
                patentproject.setEndTime(porproject.getEndTime());
            }
        }else if(Objects.nonNull(tranproject)){
            patentproject.setHead(tranproject.getHead());
            patentproject.setProjectID(tranproject.getProjectId());
            if(Objects.isNull(patentproject.getStartTime())){
                patentproject.setStartTime(tranproject.getStartTime());
            }
            if(Objects.isNull(patentproject.getEndTime())){
                patentproject.setEndTime(tranproject.getEndTime());
            }
        }else {
            throw new BaseException("项目不存在!");
        }


        //判断该项目是否已经添加过在该专利中
        List<Patentproject> patentprojectList = query().eq("projectName", patentproject.getProjectName())
                .eq("PatentID", patentproject.getPatentID()).list();
        if(!patentprojectList.isEmpty()){
            throw new BaseException("该项目已经添加过在该专利中!");
        }

        return baseMapper.insert(patentproject) == 1;
    }

    /**
     * 删除专利项目
     * @param patentProjectID 专利项目编号
     * @return 删除结果
     */
    @Override
    public boolean removePatentProject(String patentProjectID) {
        Patentproject patentproject = super.getOne(Wrappers.lambdaQuery(Patentproject.class)
                .eq(Patentproject::getPatentProjectID, patentProjectID));
        if(Objects.isNull(patentproject)){
            throw new BaseException("不存在该关联项目");
        }

        //拦截普通用户在非法状态修改的操作
        Patent patent = iPatentService.getOne(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getPatentID, patentproject.getPatentID()));
        checkRoleUtil.checkPermissionAchievement(patent.getStatus());

        return baseMapper.deleteById(patentproject) == 1;



    }

    /**
     * 修改专利项目
     * @param patentproject
     * @return
     */
    @Transactional
    @Override
    public boolean updatePatentProject(Patentproject patentproject) {
        Patent patent = iPatentService.getOne(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getPatentID, patentproject.getPatentID()));
        checkRoleUtil.checkPermissionAchievement(patent.getStatus());

        //删除之前的项目
        removeById(patentproject.getPatentProjectID());

        //新增当前项目
        addPatentProject(patentproject);

        return true;
    }

    /**
     * 根据专利编号查询
     * @param patentID
     * @return
     */
    @Override
    public List<Patentproject> getPatentProjectByPatentID(String patentID) {
        Patent byId = iPatentService.getById(patentID);
        if (Objects.isNull(byId)){
            throw new BaseException("输入的专利编号有误!");
        }
        List<Patentproject> patentprojects = baseMapper.selectList(Wrappers.lambdaQuery(Patentproject.class)
                .eq(Patentproject::getPatentID, patentID));
        //将head置的id转为名称
        for (Patentproject patentproject : patentprojects) {
            String head = patentproject.getHead();
            User user = iUserService.getById(head);
            patentproject.setHead(user.getName());
        }
        return patentprojects;
    }

}
